package cn.itcast.bos.service.take_delivery.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.PromotionDao;
import cn.itcast.bos.domain.take_delivery.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;


//添加注解，交给spring管理
@Service
@Transactional//添加事务控制
public class PromotionServiceImpl implements PromotionService {

//注入dao层
	@Autowired
	private PromotionDao PromotionDao;
	
	@Override
	public void save(Promotion promotion) {
		PromotionDao.save(promotion);

	}

	@Override
	public Page<Promotion> pageQuery(Pageable pageable) {
		Page<Promotion> page = PromotionDao.findAll(pageable);
		return page;
	}

	@Override
	public PageBean<Promotion> pageQuery(Integer page, Integer rows) {
		
		Pageable pageable=new PageRequest(page-1, rows);
		Page<Promotion> page2 = PromotionDao.findAll(pageable);
		
		//将数据封装到pageBean中
		PageBean<Promotion> pageBean=new PageBean<Promotion>();
		pageBean.setPageData(page2.getContent());
		pageBean.setTotalCount(page2.getTotalElements());
		return pageBean;
	}

	@Override
	public Promotion findPomotionById(Integer id) {
		Promotion promotion = PromotionDao.findOne(id);
		return promotion;
	}

	@Override
	public void updateStatus(Date date) {
		PromotionDao.updateStatus(date);
		
	}

}
